package com.wedo.mall.activity.payment;

import android.os.Bundle;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.wedo.mall.R;
import com.wedo.mall.activity.MainActivity;
import com.wedo.mall.activity.user.OrderDetailActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * Created by e on 2017/8/8.
 */

public class PaySuccessActivity extends EActivity {

    @BindView(R.id.tv_order) TextView tv_order;
    @BindView(R.id.tv_coin) TextView tv_coin;

    private String order;

    @BindString(R.string.order_sn) String order_sn;
    @BindString(R.string.order_point_unit) String order_point_unit;

    @Override
    protected int layout() {
        return R.layout.activity_pay_success;
    }

    @Override
    protected void onActivityCreated(Bundle state) {

        order = getIntent().getStringExtra(PayActivity.EXTRA_ORDER);
        tv_order.setText(String.format(order_sn,order));

        Map<String,String> params = new HashMap<>(1);
        params.put("order_sn",order);
        loading();
        Observable<EResponse> observable = api().post(EApi.PAYMENT_NOTIF,params);
        observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                Map<String,String> map = response.object("orderInfo",Map.class);
                tv_coin.setText(String.format(order_point_unit,map.get("giving_point")));
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });

    }

    @OnClick(R.id.btn_order)
    protected void onOrderClick(){
        OrderDetailActivity.toDetail(this,order);
    }

    @OnClick(R.id.btn_back)
    protected void onBackClick(){
        MainActivity.toMain(this);
    }
}
